/**
 * @class   wosh::gui::WoshTreeModel
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: WoshTreeModel.h 2305 2010-04-15 21:27:18Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WoshTreeModel_H__
 #define __WOSH_Gui_WoshTreeModel_H__

 #include <core/Tree.h>
 #include <QAbstractItemModel>


namespace wosh {
 namespace gui {


class WoshTreeModel :  public QAbstractItemModel
 {
	Q_OBJECT

	public:
		WoshTreeModel( Tree* tree_data, QObject *parent = 0) : QAbstractItemModel(parent) {
			this->tree = NULL;
			this->fetchMoreEnabled = false;
			loadTree( tree_data );
		 }

		virtual ~WoshTreeModel() { }

	public:
		void loadTree( const wosh::Tree* tree_data ) {
			if ( this->tree != NULL )
				delete this->tree; this->tree = NULL;
			if ( tree_data == NULL )
				return;
			this->tree = tree_data->clone();
			reset();
		 }
		inline void setFetchMore( bool value )			{ this->fetchMoreEnabled = value; }

		inline TreeNode* getRootNode() const {
			if ( this->tree == NULL ) return NULL;
			return this->tree->getRoot();
		 }

	public:
		int rowCount( const QModelIndex &parent = QModelIndex() ) const {
			TreeNode *parentItem = NULL;
			if (!parent.isValid())
				parentItem = getRootNode();
			else
				parentItem = static_cast<TreeNode*>(parent.internalPointer());
			if ( parentItem == NULL )
				return 0;
			return parentItem->getChildren().size();
		 }

		virtual QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const {
			if (!hasIndex(row, column, parent))
				return QModelIndex();
			TreeNode* parentItem = NULL;
			if (!parent.isValid())
				parentItem = getRootNode();
			else
				parentItem = static_cast<TreeNode*>(parent.internalPointer());
			if ( parentItem == NULL )
				return QModelIndex();

			TreeNode* childItem = reinterpret_cast<TreeNode*>(parentItem->getChildren().itemAt(row));
			if (childItem)
				return createIndex(row, column, childItem);
			else
				return QModelIndex();
		 }

		virtual QModelIndex parent( const QModelIndex &child ) const {
			 if ( !child.isValid() )
				 return QModelIndex();
		    TreeNode* childItem = static_cast<TreeNode*>(child.internalPointer());
			if ( childItem == NULL )
				return QModelIndex();

			TreeNode* parentItem = reinterpret_cast<TreeNode*>(childItem->getParent());
			if ( parentItem == getRootNode() )
				return QModelIndex();
			if ( parentItem == NULL )
				return QModelIndex();
			TreeNode* parentParentItem = reinterpret_cast<TreeNode*>(parentItem->getParent());
			if ( parentParentItem == NULL )
				return createIndex(1,0, parentItem);
			int childIndex = parentParentItem->getChildren().indexOf(parentItem);
			if( childIndex < 0 ) childIndex = 0;
			return createIndex(childIndex,0, parentItem);
		 }

		Qt::ItemFlags flags( const QModelIndex &index ) const {
			if ( !index.isValid() ) return 0;
			return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
		 }

	public: // sortable
		void sort ( int column, Qt::SortOrder  order = Qt::AscendingOrder ) {
			if ( this->tree == NULL ) return;
			if ( column < 0 ) return;
			//TODO
			(void)order;
		 }

	public:
		bool canFetchMore( const QModelIndex & parent ) const {
			if ( !this->fetchMoreEnabled ) return false;
			TreeNode* item = static_cast<TreeNode*>(parent.internalPointer());
			if ( item == NULL ) return false;
			return false;
		 }
		void fetchMore( const QModelIndex & parent ) {
			emit s_requestChildrenOf( parent );
		 }

	signals:
		void s_requestChildrenOf( const QModelIndex & item );

	protected:
		mutable Tree* tree;
		bool fetchMoreEnabled;

}; // class def



 }; // namespace gui
}; // namespace wosh

#endif //__WOSH_Gui_WoshTreeModel_H__
